/**
 * Copyright (C) Skywares Information Technology, LTD. 
 * All Rights Reserved.
 *
 * DataSourceConfigurer.java created on 2020年2月11日 上午11:27:14 by Lyon Lu 
 */
package com.mvc.server.dynamic.ds.config;

import java.util.Collections;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

/**
 * <pre>
 * Description:
 * 
 * @author Lyon Lu
 * @date 2020年2月11日 上午11:27:14
 *
 * </pre>
 */
@Configuration
public class DataSourceConfigurer {
	
	@Bean("master")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource master() {
        return DruidDataSourceBuilder.create().build();
    }
	
	@Bean("slave1")
    @ConfigurationProperties(prefix = "spring.datasource.slave-1")
    public DataSource slave1() {
        return DruidDataSourceBuilder.create().build();
    }

	/*
	 * @Bean("slave2")
	 * 
	 * @ConfigurationProperties(prefix = "spring.datasource.slave-2") public
	 * DataSource slave2() { return DruidDataSourceBuilder.create().build(); }
	 */
	
	@Bean("dataSource")
	public DataSource dynamicDataSource() {
		
		DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
		
//		  Map<Object, Object> dataSourceMap = new HashMap<>(4);
//		  dataSourceMap.put("master", master()); 
//		  dataSourceMap.put("slave1", slave1());
//		  dataSourceMap.put("slave2", slave2()); 
		  // 将 master 和 slave 数据源作为指定的数据源
		  dynamicRoutingDataSource.setTargetDataSources(Collections.emptyMap());
		 

        // 将 master 数据源作为默认指定的数据源
        dynamicRoutingDataSource.setDefaultTargetDataSource(master());
        

        return dynamicRoutingDataSource;
	}
	
	/**
	 * Description:
	 * 		method description here
	 * @param @param dataSource
	 * @param @throws Exception
	 * @return SqlSessionFactoryBean      
	 * @throws
	 */
	@Bean
	@ConfigurationProperties(prefix = "mybatis")
	public SqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource)
			throws Exception {
		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSource);
		
		return factoryBean;
	}
	
	/**
	 * Description:
	 * 		注入 DataSourceTransactionManager 用于事务管理
	 * @param @param dataSource
	 * @return PlatformTransactionManager      
	 * @throws
	 */
    @Bean
    public PlatformTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
